package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.exception.CustomException;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.domain.SysProj;
import com.xjy.system.domain.bo.SysMaterialMngAddBo;
import com.xjy.system.domain.bo.SysMaterialMngEditBo;
import com.xjy.system.domain.bo.SysMaterialMngQueryBo;
import com.xjy.system.domain.vo.SysMaterialMngVo;
import com.xjy.system.domain.vo.SysProjVo;
import com.xjy.system.service.ISysProjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.SysMaterialMng;
import com.xjy.system.mapper.SysMaterialMngMapper;
import com.xjy.system.service.ISysMaterialMngService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物料管理Service业务层处理
 *
 * @author zc
 * @date 2022-01-30
 */
@Service
public class SysMaterialMngServiceImpl implements ISysMaterialMngService {
	@Autowired
	private ISysProjService iSysProjService;
	@Autowired
	private SysMaterialMngMapper sysMaterialMngMapper;

	@Override
	public SysMaterialMngVo queryById(Long id) {
		SysMaterialMngVo rsp = sysMaterialMngMapper.queryDetailById(id);
		return rsp;

	}

	@Override
	public TableDataInfo<SysMaterialMngVo> queryPageList(SysMaterialMngQueryBo bo) {
		List<SysMaterialMngVo> sysMaterialMngVoList = sysMaterialMngMapper.queryList(bo);
		return PageUtils.getPageInfo(sysMaterialMngVoList);
	}

	@Override
	public List<SysMaterialMngVo> queryList(SysMaterialMngQueryBo bo) {
		List<SysMaterialMngVo> sysMaterialMngVoList = sysMaterialMngMapper.queryList(bo);
		return sysMaterialMngVoList;
	}


	@Override
	public Boolean insertByAddBo(SysMaterialMngAddBo bo) {
		SysMaterialMng add = BeanUtil.toBean(bo, SysMaterialMng.class);
		Long startAmt = add.getStartAmt();
		if (null == startAmt || !(startAmt >= 0)) {
			throw new UtilException("期初余额必须大于等于0！");
		}
		add.setInitAmt(add.getStartAmt());
		add.setAvailableStock(add.getStartAmt());
		return sysMaterialMngMapper.insert(add);
	}

	@Override
	public Boolean updateByEditBo(SysMaterialMngEditBo bo) {
		SysMaterialMng update = BeanUtil.toBean(bo, SysMaterialMng.class);
		Long startAmt = update.getStartAmt();
		if (null == startAmt || !(startAmt >= 0)) {
			throw new CustomException("期初余额必须大于等于0！");
		}

		return sysMaterialMngMapper.update(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysMaterialMng entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return sysMaterialMngMapper.deleteByIds(ids);
	}

	@Override
	public Boolean updateByEditVo(SysMaterialMngVo sysMaterialMngVo) {
		SysMaterialMng update = BeanUtil.toBean(sysMaterialMngVo, SysMaterialMng.class);
		return sysMaterialMngMapper.update(update);
	}
}
